package cn.xuchunh.campusbbs.ui.fragments.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import cn.xuchunh.campusbbs.AppContext;
import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.ui.activities.LoginActivity;
import cn.xuchunh.campusbbs.ui.activities.MainActivity;
import cn.xuchunh.campusbbs.listener.DialogNegativeListener;
import cn.xuchunh.campusbbs.net.HttpRequestManager;
import cn.xuchunh.campusbbs.storage.sp.SharedPreferenceManager;
import cn.xuchunh.campusbbs.utils.Constants;

/**
 * Created by XuChunH on 2015/10/11.
 */
public class ServerSettingDialog extends AppCompatDialogFragment {

    private EditText domain;
    private EditText forumKey;
    private EditText appName;
    private EditText sdkVersion;
    private EditText packageName;

    private SharedPreferenceManager sharedPreferenceManager = SharedPreferenceManager
            .getInstance();

    /**
     *
     * 若使用create方法，view 不能在onCreateView中inflate，否则在显示AlertDialog时将抛出如下异常
     * "android.util.AndroidRuntimeException: Window feature must be requested before adding content"。
     *
     * 若使用show方法，view 能在onCreateView中inflate，但AlertDialog的Title和Button将不显示
     *
     * @param savedInstanceState
     * @return
     */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View contentView = LayoutInflater.from(getActivity())
                .inflate(R.layout.layout_setting_server, null, false);
        domain = (EditText) contentView.findViewById(R.id
                .server_domain);
        forumKey = (EditText) contentView.findViewById(R.id
                .app_forumkey);
        appName = (EditText) contentView.findViewById(R.id.app_name);
        sdkVersion = (EditText) contentView.findViewById(R.id
                .app_sdk_version);
        packageName = (EditText) contentView.findViewById(R.id
                .app_package_name);
        return builder.setTitle(R.string.setting_server)
                .setView(contentView)
                .setPositiveButton(R.string.confirm, positiveLisenter)
                .setNegativeButton(R.string.cancel, new DialogNegativeListener())
                .create();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        domain.setText(sharedPreferenceManager.getServerDomain());
        forumKey.setText(sharedPreferenceManager.getForumKey());
        appName.setText(sharedPreferenceManager.getAppName());
        sdkVersion.setText(sharedPreferenceManager.getSdkVersion());
        packageName.setText(sharedPreferenceManager.getPackageName());
        return null;
    }

    private OnClickListener positiveLisenter = new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (TextUtils.isEmpty(domain.getEditableText().toString())
                    || TextUtils.isEmpty(forumKey.getEditableText().toString())
                    || TextUtils.isEmpty(appName.getEditableText().toString())
                    || TextUtils.isEmpty(sdkVersion.getEditableText().toString())
                    || TextUtils.isEmpty(packageName.getEditableText().toString())) {

                Toast.makeText(getActivity(), "必须填写所用项目", Toast.LENGTH_SHORT).show();
                return;
            }

            boolean changed = false;
            dialog.dismiss();

            String content = domain.getEditableText().toString();
            if (!content.equals(sharedPreferenceManager.getServerDomain())) {
                changed = true;
                sharedPreferenceManager.setServerDomain(content);
            }

            content = forumKey.getEditableText().toString();
            if (!content.equals(sharedPreferenceManager.getForumKey())) {
                changed = true;
                sharedPreferenceManager.setForumKey(content);
            }

            content = appName.getEditableText().toString();
            if (!content.equals(sharedPreferenceManager.getAppName())) {
                changed = true;
                sharedPreferenceManager.setAppName(content);
            }

            content = sdkVersion.getEditableText().toString();
            if (!content.equals(sharedPreferenceManager.getSdkVersion())) {
                changed = true;
                sharedPreferenceManager.setSdkVersion(content);
            }

            content = packageName.getEditableText().toString();
            if (!content.equals(sharedPreferenceManager.getPackageName())) {
                changed = true;
                sharedPreferenceManager.setPackageName(content);
            }

            if (changed) {
                HttpRequestManager.getInstance(AppContext.getInstance()).stop();
                LoginActivity.launch(getActivity());
                getActivity().finish();
                if (sharedPreferenceManager.getUiMode() == Constants.UI_MODE_BOTTOM
                        && MainActivity.instance != null) {
                    MainActivity.instance.finish();
                }
            }

        }
    };

}
